package com.yq.cp.service.basics.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.cp.config.MyException;
import com.yq.cp.entity.basics.Department;
import com.yq.cp.entity.basics.OrganizationalStructure;
import com.yq.cp.entity.basics.Position;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.entity.permission.RolePermission;
import com.yq.cp.entity.permission.User;
import com.yq.cp.entity.permission.dto.PermissionDto;
import com.yq.cp.mapper.basics.DepartmentMapper;
import com.yq.cp.mapper.basics.OrganizationalStructureMapper;
import com.yq.cp.mapper.basics.PositionMapper;
import com.yq.cp.mapper.permission.PermissionMapper;
import com.yq.cp.mapper.permission.RolePermissionMapper;
import com.yq.cp.mapper.permission.UserMapper;
import com.yq.cp.service.basics.PositionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 职位表 服务实现类
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionService {
    @Resource
    PositionMapper positionMapper;
    @Resource
    RolePermissionMapper rolePermissionMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    DepartmentMapper departmentMapper;
    @Resource
    OrganizationalStructureMapper organizationalStructureMapper;
    @Resource
    PermissionMapper permissionMapper;
    @Override
    public void add(Position position) {
        if (Objects.isNull(position.getId())){
            positionMapper.insert(position);
        }else {
            positionMapper.updateById(position);
            rolePermissionMapper.deleteByRoleId(position.getId());
        }
        if (!Objects.isNull(position.getNumbersManage())&&!position.getNumbersManage().isEmpty()) {
            for (Long number : position.getNumbersManage()) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setPermissionId(number);
                rolePermission.setRoleId(position.getId());
                rolePermissionMapper.insert(rolePermission);
            }
        }
        if (!Objects.isNull(position.getNumbersUser())&&!position.getNumbersUser().isEmpty()) {
        for (Long number : position.getNumbersUser()) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setPermissionId(number);
            rolePermission.setRoleId(position.getId());
            rolePermissionMapper.insert(rolePermission);
            }
        }
        if (!Objects.isNull(position.getNumbersdashuju())&&!position.getNumbersdashuju().isEmpty()) {
            for (Long number : position.getNumbersdashuju()) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setPermissionId(number);
                rolePermission.setRoleId(position.getId());
                rolePermissionMapper.insert(rolePermission);
            }
        }
    }

    @Override
    public void delete(Position position) {
        rolePermissionMapper.deleteByRoleId(position.getId());
        positionMapper.deleteById(position);
    }

    @Override
    public Page<Position> listToPosition(PageVo vo) {
        Page<Position> page = new Page<>(vo.getPage(), vo.getSize());
        page = positionMapper.listToPosition(page,vo);
        if (!page.getRecords().isEmpty()) {
            for (Position record : page.getRecords()) {
                List<PermissionDto> permissions = permissionMapper.queryAllByRoleId(0L, record.getId()
                        , 1);
                List<PermissionDto> permissions1 = permissionMapper.queryAllByRoleId(0L, record.getId()
                        , 2);
                List<PermissionDto> permissions2 = permissionMapper.queryAllByRoleId(0L, record.getId()
                        , 3);
                record.setManage(permissions);
                record.setUser(permissions1);
                record.setDashuju(permissions2);
            }


        }
        return page;
    }

    @Override
    public User listUserToPosition(User vo) {
        User user = userMapper.selectOne(new QueryWrapper<User>()
                .eq("nickname", vo.getNickname()).eq("phone", vo.getPhone())
                .eq("site_id",vo.getId()));
        if (user==null || Objects.isNull(user.getPositionId()))
            throw new MyException("未查询到职位，请重新输入后再试！！！");
        Position position = positionMapper.selectById(user.getPositionId());
        Department department = departmentMapper.selectById(user.getDepartmentId());
        OrganizationalStructure structure = organizationalStructureMapper.selectById(user.getSiteId());
        OrganizationalStructure structure1 = organizationalStructureMapper.selectById(structure.getParentId());
        OrganizationalStructure structure2 = organizationalStructureMapper.selectById(structure1.getParentId());
        OrganizationalStructure structure3 = organizationalStructureMapper.selectById(structure2.getParentId());
        OrganizationalStructure structure4 = organizationalStructureMapper.selectById(structure3.getParentId());
        OrganizationalStructure structure5= organizationalStructureMapper.selectById(structure4.getParentId());
        user.setPositionName(position.getName());
        user.setDepartmentName(department.getName());
        user.setSiteName(structure.getName());
        user.setQuId(structure1.getId());
        user.setQuName(structure1.getName());
        user.setShiId(structure2.getId());
        user.setShiName(structure2.getName());
        user.setShengId(structure3.getId());
        user.setShengName(structure3.getName());
        user.setDaquId(structure4.getId());
        user.setDaquName(structure4.getName());
        user.setQudaoshangId(structure5.getId());
        user.setQudaoshangName(structure5.getName());
        return user;
    }
}
